package com.ruoyi.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.domain.mes.MesCoveringOrder;
import com.ruoyi.web.domain.mes.MesCoveringOrderUser;
import com.ruoyi.web.domain.mes.MesCoveringOrderWork;
import com.ruoyi.web.mapper.CoveringOrderMapper;
import com.ruoyi.web.mapper.MesConfigMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CoveringOrderService {
    @Resource
    private CoveringOrderMapper coveringOrderMapper;
    @Resource
    private MesConfigMapper configMapper;

    /**
     * 覆膜日报单 -- 查询主表
     * @Date: 2024年4月9日
     * @Auth: 张恒
     */
    public TableDataInfo list(MesCoveringOrder coveringOrder){
        PageHelper.startPage(coveringOrder.getPageNum(), coveringOrder.getPageSize());
        List<MesCoveringOrder> mesCoveringOrders = coveringOrderMapper.selectCoveringOrder(coveringOrder);
        PageInfo pageInfo = new PageInfo<>(mesCoveringOrders);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setTotal(pageInfo.getTotal());
        tableDataInfo.setRows(pageInfo.getList());
        return tableDataInfo;
    }

    /**
     * 覆膜日报单 -- 查询主细表
     * @Date: 2024年4月9日
     * @Auth: 张恒
     */
    public AjaxResult getById(MesCoveringOrder coveringOrder){
        // 查询主表信息
        List<MesCoveringOrder> mesCoveringOrders = coveringOrderMapper.selectCoveringOrder(coveringOrder);
        MesCoveringOrder mesCoveringOrder = mesCoveringOrders.get(0);
        // 查询人员信息
        List<MesCoveringOrderUser> mesCoveringOrderUsers = coveringOrderMapper.selectCoveringOrderUser(coveringOrder);
        // 查询工作内容
        List<MesCoveringOrderWork> mesCoveringOrderWorks = coveringOrderMapper.selectCoveringOrderWork(coveringOrder);
        // 合并数据
        mesCoveringOrder.setOrderUserList(mesCoveringOrderUsers);
        mesCoveringOrder.setOrderWorkList(mesCoveringOrderWorks);
        return AjaxResult.success(mesCoveringOrder);
    }

    /**
     * 覆膜日报单 -- 新增
     * @Date: 2024年4月9日
     * @Auth: 张恒
     */
    @Transactional
    public AjaxResult add(MesCoveringOrder coveringOrder){
        String username = SecurityUtils.getUsername();
        Date date = new Date();
        coveringOrder.setPid(UUID.randomUUID().toString().replace("-", ""));
        coveringOrder.setCreateBy(username);
        coveringOrder.setCreateTime(date);
        // 获取编号
        Map<String, Object> map = new HashMap<>();
        map.put("columnName", "cover_no");
        map.put("tableName", "mes_covering_order");
        int maxNum = configMapper.getMouthMaxId(map);
        String code = PinYinUtils.getCodeNormal(maxNum, "FMRBD");
        coveringOrder.setCoverNo(code);

        // 查询维修保养记录

        // 新增主表信息
        coveringOrderMapper.insertCoveringOrder(coveringOrder);
        // 新增人员信息
        if(coveringOrder.getOrderUserList() != null && coveringOrder.getOrderUserList().size() > 0){
            coveringOrder.getOrderUserList().forEach(e ->{
                e.setPid(UUID.randomUUID().toString().replace("-", ""));
                e.setOrderId(coveringOrder.getPid());
            });
            coveringOrderMapper.insertCoveringOrderUser(coveringOrder.getOrderUserList());
        }
        // 新增工作内容
        if(coveringOrder.getOrderWorkList() != null && coveringOrder.getOrderWorkList().size() > 0){
            coveringOrder.getOrderWorkList().forEach(e ->{
                e.setPid(UUID.randomUUID().toString().replace("-", ""));
                e.setOrderId(coveringOrder.getPid());
            });
            coveringOrderMapper.insertCoveringOrderWork(coveringOrder.getOrderWorkList());
        }
        return AjaxResult.success();
    }

    /**
     * 覆膜日报单 -- 修改
     * @Date: 2024年4月9日
     * @Auth: 张恒
     */
    @Transactional
    public AjaxResult edit(MesCoveringOrder coveringOrder){
        if(StringUtils.isBlank(coveringOrder.getPid())){
            return AjaxResult.error("未获取到关键信息");
        }
        String username = SecurityUtils.getUsername();
        Date date = new Date();
        coveringOrder.setUpdateBy(username);
        coveringOrder.setUpdateTime(date);
        // 修改主表信息
        coveringOrderMapper.updateCoveringOrder(coveringOrder);
        // 修改人员信息
        if(coveringOrder.getOrderUserList() != null && coveringOrder.getOrderUserList().size() > 0){
            coveringOrder.getOrderUserList().forEach(e ->{
                e.setPid(UUID.randomUUID().toString().replace("-", ""));
                e.setOrderId(coveringOrder.getPid());
            });
            // 删除历史数据
            coveringOrderMapper.delCoveringOrderUser(coveringOrder);
            // 新增
            coveringOrderMapper.insertCoveringOrderUser(coveringOrder.getOrderUserList());
        }
        // 修改工作内容
        if(coveringOrder.getOrderWorkList() != null && coveringOrder.getOrderWorkList().size() > 0){
            coveringOrder.getOrderWorkList().forEach(e ->{
                e.setPid(UUID.randomUUID().toString().replace("-", ""));
                e.setOrderId(coveringOrder.getPid());
            });
            // 删除历史数据
            coveringOrderMapper.delCoveringOrderWork(coveringOrder);
            // 新增
            coveringOrderMapper.insertCoveringOrderWork(coveringOrder.getOrderWorkList());
        }
        return AjaxResult.success();
    }

    /**
     * 覆膜日报单 -- 删除
     * @Date: 2024年4月9日
     * @Auth: 张恒
     */
    @Transactional
    public AjaxResult del(MesCoveringOrder coveringOrder){
        if(StringUtils.isBlank(coveringOrder.getPid())){
            return AjaxResult.error("未获取到关键信息");
        }
        // 删除主表
        coveringOrderMapper.delCoveringOrder(coveringOrder);
        // 删除人员信息
        coveringOrderMapper.delCoveringOrderUser(coveringOrder);
        // 删除工作内容
        coveringOrderMapper.delCoveringOrderWork(coveringOrder);
        return AjaxResult.success();
    }
}
